﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using ComboBox = System.Windows.Forms.ComboBox;

namespace WinUtilities.Class
{
    public static partial class GlobalUtilities
    {
        public static void ControlEnable(bool Enabled, params Control[] listControl)
        {
            if (Enabled == false)
            {
                foreach (Control control in listControl)
                {
                    control.Enabled = false;
                }
            }
            else
            {
                foreach (Control control in listControl)
                {
                    control.Enabled = true;
                }
            }
        }
        public static void ControlEnable(bool Enabled, params LayoutControl[] layoutControls)
        {
            if (Enabled == false)
            {
                foreach (LayoutControl layoutControl in layoutControls)
                {
                    foreach (LayoutControlItem item in layoutControl.Root.Items)
                    {
                        item.Control.Enabled = false;
                    }

                }
            }
            else
            {
                foreach (LayoutControl layoutControl in layoutControls)
                {
                    foreach (LayoutControlItem item in layoutControl.Root.Items)
                    {
                        item.Control.Enabled = true;
                    }

                }
            }
        }
        public static void ClearTextbox(params LayoutControl[] layoutControls)
        {
            var editValueControls = new List<Type>();
            editValueControls.Add(typeof(DateEdit));
            editValueControls.Add(typeof(LookUpEdit));
            editValueControls.Add(typeof(SearchLookUpEdit));
            foreach (LayoutControl layoutControl in layoutControls)
            {
                foreach (LayoutControlItem item in layoutControl.Root.Items)
                {
                    if (item.Control.GetType() == typeof(TextBox))
                        item.Control.Text = "";

                    else if (editValueControls.Contains(item.Control.GetType()))
                    {
                        var editValue = item.Control.GetType().GetProperty("EditValue");
                        editValue.SetValue(item.Control, null, null);
                    }
                    else if (item.Control.GetType() == typeof(ComboBox))
                    {
                        ((ComboBox)item.Control).SelectedIndex = -1;
                    }
                }
            }
        }
        public static void SearchLookUpEditFirstKeyPress(object sender, KeyPressEventArgs e)
        {

            if (Char.IsLetterOrDigit(e.KeyChar))

            {
                PropertyInfo isPopupOpen = sender.GetType().GetProperty("IsPopupOpen");
                if (!(bool)isPopupOpen.GetValue(sender, null))
                {
                    var showPopupMethod = sender.GetType().GetMethod("ShowPopup");
                    showPopupMethod.Invoke(sender,null);
                    SendKeys.Send(e.KeyChar.ToString());
                    e.Handled = true;
                }
            }
        }

        public static bool CheckInfo(List<Control> requireValue, params LayoutControl[] layoutControls)
        {
            const string message = "Please input ";
            foreach (LayoutControl layoutControl in layoutControls)
            {
                foreach (LayoutControlItem item in layoutControl.Root.Items)
                {
                    if (requireValue.Contains(item.Control))
                    {
                        if (item.Control.GetType() == typeof(TextBox))
                        {
                            if (item.Control.Text == "")
                            {
                                MessageBox.Show(message + item.CustomizationFormText);
                                item.Control.Focus();
                                return false;
                            }
                        }
                        if (item.Control.GetType() == typeof(ComboBox))
                        {
                            if (((ComboBox)item.Control).SelectedItem == null)
                            {
                                MessageBox.Show(message + item.CustomizationFormText);
                                item.Control.Focus();
                                return false;
                            }
                        }
                        if (item.Control.GetType() == typeof(DateEdit))
                        {
                            var control = (DateEdit)item.Control;
                            if (control.EditValue == null)
                            {
                                MessageBox.Show(message + item.CustomizationFormText);
                                control.Focus();
                                return false;
                            }
                        }
                        if (item.Control.GetType() == typeof(LookUpEdit))
                        {
                            var control = (LookUpEdit)item.Control;
                            if (control.EditValue == null)
                            {
                                MessageBox.Show(message + item.CustomizationFormText);
                                control.Focus();
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
        // Process form

        [DllImport("user32.dll")]
        private static extern IntPtr GetFocus();

        public delegate bool FormHotKey(Keys keyData);

        public static bool GlobalProcessCmdKey(XtraForm formBase,FormHotKey formHotKeyMethod, ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                IntPtr wndHandle = GetFocus();
                Control focusedControl = Control.FromChildHandle(wndHandle);
                if (focusedControl is TextBox)
                {
                    SendKeys.Send("{Tab}");
                    return true;
                }
                if (focusedControl is ComboBox)
                {
                    SendKeys.Send("{Tab}");
                    return true;
                }
                var sle = focusedControl as SearchLookUpEdit;
                if (sle != null)
                {
                    if (!sle.IsPopupOpen)
                    {
                        SendKeys.Send("{Tab}");
                        return true;
                    }
                    
                }
                if (focusedControl is SimpleButton)
                {
                    SendKeys.Send(" ");
                    return true;
                }
            }
            return formHotKeyMethod(keyData);
        }
    }
}
